﻿Imports Controle
Imports UtilPadrao
Public Class frmConsultaProposta

#Region "Variáveis"
    Private sCodCliente As String

    Private objCliente As New ctrCliente
    Private objProposta As New ctrProposta

#End Region

#Region "Propriedades"

    Private Property CodCliente As String
        Get
            Return sCodCliente
        End Get
        Set(ByVal value As String)
            sCodCliente = value
            Me.preencherCliente()
        End Set
    End Property

#End Region

#Region "Enumerações"

    Private Enum eColsConsulta As Int32
        codProposta = 0
        cotacao = 1
        revisao = 2
        cliente = 3
        dataProposta = 4
        dataValidade = 5
        valorProposta = 6
    End Enum

#End Region

    Private Sub executarConsulta()
        Dim dtbConsulta As New DataTable
        Dim iCodProposta As Int32
        Dim iRevisao As Int32 = 0
        Dim dtInicio As Date = Date.MinValue
        Dim dtFim As Date = Date.MinValue
        Dim dtInicioValidade As Date = Date.MinValue
        Dim dtFimValidade As Date = Date.MinValue
        Dim dtAprovacao As Date = Date.MinValue

        Try
            If txtCodCotacao.Text <> String.Empty Then
                iCodProposta = Me.objProposta.retornarCodProposta(txtCodCotacao.Text)
            Else
                iCodProposta = -1
            End If

            If Not chkTodas.Checked Then
                iRevisao = conversao.toInt32(txtRevisao.Text)
            Else
                iRevisao = -1
            End If

            If dtpInicio.Checked Then
                dtInicio = dtpInicio.Value
            End If

            If dtpFim.Checked Then
                dtFim = dtpFim.Value
            End If

            If dtpInicioValidade.Checked Then
                dtInicioValidade = dtpInicioValidade.Value
            End If

            If dtpFimValidade.Checked Then
                dtFimValidade = dtpFimValidade.Value
            End If

            If dtpAprovacao.Checked Then
                dtAprovacao = dtpAprovacao.Value
            End If

            dtbConsulta = objProposta.retornarPropostas(iCodProposta,
                                                        iRevisao,
                                                        dtInicio,
                                                        dtFim,
                                                        dtInicioValidade,
                                                        dtFimValidade,
                                                        Me.CodCliente,
                                                        Me.cmbStatus.SelectedIndex,
                                                        dtAprovacao)
            dgvConsulta.DataSource = dtbConsulta

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

    Private Sub configurarGridProposta()

        With dgvConsulta
            .DataSource = Nothing
            .AutoGenerateColumns = False
            .AllowUserToAddRows = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .RowHeadersVisible = False
            .Columns.Clear()

            Dim colCodProposta As New DataGridViewTextBoxColumn()
            With colCodProposta
                .HeaderText = "CodProposta"
                .DataPropertyName = "codProposta"
                .ReadOnly = True
                .Visible = False
                .Frozen = True
                .Width = 120
                .Visible = False
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                .ValueType = System.Type.GetType("System.integer")
            End With
            .Columns.Add(colCodProposta)

            Dim colCodCotacao As New DataGridViewTextBoxColumn()
            With colCodCotacao
                .HeaderText = "Cotação"
                .DataPropertyName = "Cotacao"
                .ReadOnly = True
                .Visible = True
                .Frozen = True
                .Width = 70
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .ValueType = System.Type.GetType("System.string")
            End With
            .Columns.Add(colCodCotacao)

            Dim colRevisao As New DataGridViewTextBoxColumn()
            With colRevisao
                .HeaderText = "Rev."
                .DataPropertyName = "revProposta"
                .ReadOnly = True
                .Visible = True
                .Frozen = True
                .Width = 35
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .ValueType = System.Type.GetType("System.string")
            End With
            .Columns.Add(colRevisao)

            Dim colCliente As New DataGridViewTextBoxColumn()
            With colCliente
                .HeaderText = "Cliente"
                .DataPropertyName = "cliente"
                .ReadOnly = True
                .Visible = True
                .Frozen = True
                .Width = 230
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                .ValueType = System.Type.GetType("System.string")
            End With
            .Columns.Add(colCliente)

            Dim colDataProposta As New DataGridViewTextBoxColumn()
            With colDataProposta
                .HeaderText = "Data"
                .DataPropertyName = "dataProposta"
                .ReadOnly = True
                .Visible = True
                .Frozen = True
                .Width = 70
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .DefaultCellStyle.Format = "dd/MM/yyyy"
                .ToolTipText = "Data Proposta"
                .ValueType = System.Type.GetType("System.date")
            End With
            .Columns.Add(colDataProposta)

            Dim colDataValidade As New DataGridViewTextBoxColumn()
            With colDataValidade
                .HeaderText = "Validade"
                .DataPropertyName = "validade"
                .ReadOnly = True
                .Visible = True
                .Frozen = True
                .Width = 70
                .DefaultCellStyle.Format = "dd/MM/yyyy"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .ValueType = System.Type.GetType("System.date")
            End With
            .Columns.Add(colDataValidade)

            Dim colValor As New DataGridViewTextBoxColumn()
            With colValor
                .HeaderText = "Valor (R$)"
                .DataPropertyName = "valorProposta"
                .ReadOnly = True
                .Visible = True
                .Frozen = True
                .Width = 130
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .ValueType = System.Type.GetType("System.decimal")
                .DefaultCellStyle.Format = "###,###,##0.00"
            End With
            .Columns.Add(colValor)

        End With

    End Sub

    Private Sub preencherStatus()
        Try
            With cmbStatus
                .Items.Clear()
                .Items.AddRange(objProposta.StatusProposta)
                .SelectedIndex = 0
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnSelecionarCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelecionarCliente.Click
        Dim sSql As String
        Dim frmDialogo As New frmPesquisa
        Dim sCodCliente As String = ""

        Try
            sSql = objCliente.sqlConsulta

            With frmDialogo
                .Sql = sSql
                .Titulo = "Pesquisar Cliente"
                .CarregaTela()
                If .DS.Tables(0).Rows.Count > 0 Then
                    .ShowDialog()
                    If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                        Me.CodCliente = conversao.nuloParaVazio(.ID)
                    End If
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                    Exit Sub
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)

        End Try
    End Sub

    Private Sub preencherCliente()
        Dim dtbCliente As New DataTable

        Try
            If Me.CodCliente.Trim <> String.Empty Then

                dtbCliente = objCliente.selecionarCliente(Me.CodCliente)

                If Not dtbCliente Is Nothing Then
                    txtCodCliente.Text = Me.CodCliente
                    txtCliente.Text = conversao.nuloParaVazio(dtbCliente.Rows(0).Item("razaoSocial")).ToString.ToUpper
                Else
                    MsgBox("Código não existente.", MsgBoxStyle.Exclamation, Application.ProductName)
                    Me.CodCliente = String.Empty
                    txtCodCliente.Text = ""
                    txtCliente.Text = ""
                End If

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

    Private Sub txtCodCliente_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCodCliente.Leave

        Me.CodCliente = Me.txtCodCliente.Text.Trim

    End Sub

    Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click

        Me.Close()

    End Sub

    Private Sub frmConsultaProposta_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.preencherStatus()
        Me.configurarGridProposta()

    End Sub

    Private Sub cmbStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbStatus.SelectedIndexChanged

        Try

            lblAprovacao.Visible = cmbStatus.SelectedIndex = ctrProposta.eStatus.Aprovada
            dtpAprovacao.Visible = cmbStatus.SelectedIndex = ctrProposta.eStatus.Aprovada

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

    Private Sub cmdPesquisa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPesquisa.Click

        Me.executarConsulta()

    End Sub

    Private Sub chkTodas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTodas.CheckedChanged
        If chkTodas.Checked Then
            txtRevisao.Text = String.Empty
        End If
    End Sub

    Private Sub txtRevisao_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRevisao.KeyPress

        chkTodas.Checked = False

    End Sub

    Private Sub cmdVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVisualizar.Click
        Dim iCodProposta As Int32
        Dim iRevProposta As Int32

        Try
            If dgvConsulta.SelectedRows.Count = 1 Then
                iCodProposta = dgvConsulta.SelectedRows(0).Cells(eColsConsulta.codProposta).Value
                iRevProposta = dgvConsulta.SelectedRows(0).Cells(eColsConsulta.revisao).Value

                Dim frm As New frmProposta
                frm.CodProposta = iCodProposta
                frm.Revisao = iRevProposta
                frm.ShowDialog()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
End Class